package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.IGetServicesDataCallBack;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jaelyn on 15/6/25.
 * 推送消息纪录
 */
public class ShowPushInfosActivity extends BaseUIActivity {

    @ViewInject(id = R.id.push_name)
    private TextView push_name;
    @ViewInject(id = R.id.goods_describe)
    private TextView goods_describe;
    @ViewInject(id = R.id.create_time)
    private TextView create_time;

    @ViewInject(id = R.id.seeCommentsDetail_tv, click = "seeCommentsDetail_tv_click")
    private TextView seeCommentsDetail_tv;

    private JsonMap map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent() == null) {
            toast.showToast(R.string.msg_server_error);
            finish();
            return;
        } else {
            setContentView(R.layout.show_goodsinfos_layout);
            setCenter_title(R.string.push_detail);
            initViewData();
        }
        setResult(RESULT_OK);
    }

    private void initViewData() {
        String extra = getIntent().getStringExtra("data");
        map = JsonParseHelper.getJsonMap(extra);
        int pushType = map.getInt("PushType", 0);
        if (pushType == 0) {
            seeCommentsDetail_tv.setVisibility(View.GONE);
        } else if (pushType == 1) {
            seeCommentsDetail_tv.setVisibility(View.VISIBLE);
        }

        push_name.setText(map.getStringNoNull("Title"));
        goods_describe.setText("\t\t\t\t" + map.getStringNoNull("Content"));
        // goods_describe.loadData(map.getString("Content"),"text/html","utf-8");
        create_time.setText(map.getString("CreateTime"));
        if (!map.getBoolean("IsRead")) {  //未读
            setRead(map.getStringNoNull("poId"));
            sendBroadcast(new Intent(Constant.ACAT_SHOPCARTNUMCHANGE));
        }
    }

    private void setRead(String id) {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("poId", id);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_GetUpdPushMessage);
        queue.setParamsNoJson(params);
        queue.setWhat(GetDataConfing.What_GetUpdPushMessage);
        queue.setCallBack(new IGetServicesDataCallBack() {
            @Override
            public void onLoaded(GetDataQueue entity) {
                LogUtil.d(TAG, entity.getInfo());
            }
        });
        getDataUtil.getData(queue);
    }

    private void getGoodsInfs() {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("PushMessageId", getIntent().getStringExtra("PUSHMSGID"));
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_GetPushMessageDetails);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {

                JsonMap map = JsonParseHelper.getJsonMap(entity.getInfo());
                push_name.setText(map.getString("Title"));
                // goods_describe.loadData(map.getString("Content"));
                create_time.setText(map.getString("CreateTime"));
            } else {
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }
    };

    /**
     * 馋看品论回复
     *
     * @param view
     */
    public void seeCommentsDetail_tv_click(View view) {
        Intent intent = new Intent(this, ArticalCommentsActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, map.getStringNoNull("P1"));
        intent.putExtra(ExtraKeys.Key_Msg2, getString(R.string.find_detail_all_comments));
        intent.putExtra(ExtraKeys.Key_Msg4, map.getStringNoNull("P2"));
        intent.putExtra(ExtraKeys.Key_Msg3, 1);
        setResult(RESULT_OK);
        jumpTo(intent, true);
    }

}